// pages/showHelp/showHelp.js
const app=getApp();
Page({

      /**
       * 页面的初始数据
       */
      data: {
            id:'',
            helpInfo:{},
            commentList:[],
            comment:'',
            userId:'',
            host:app.globalData.host,
            inputBottom:'0',
            inputFocus:false
      },
      showImg(e){
            let current=this.data.host+this.data.helpInfo.imgArr[e.currentTarget.dataset.index];
            let imgArr=[];
            this.data.helpInfo.imgArr.forEach(item=>{
                  imgArr.push(this.data.host+item)
            })
            wx.previewImage({
                  current: current, 
                  urls: imgArr
            })
      },
      /**根据ID删除互助信息 */
      delHelp(){
            console.log();
            wx.showModal({
                  title: '提示',
                  content: '是否删除该互助？',
                  success:res=> {
                        if (res.confirm) {
                              wx.request({
                                    method:'DELETE',
                                    url: app.globalData.host+'/client/user/delHelp',
                                    header:{
                                          'content-type': 'application/x-www-form-urlencoded'
                                    },
                                    data:{
                                          id:this.data.helpInfo.id
                                    },
                                    success:()=>{
                                                wx.showToast({
                                                  title: '删除成功',
                                                })
                                                wx.navigateTo({
                                                  url: '/pages/helpEach/helpEach',
                                                })
                                    },
                                    fail:()=>{
                                          wx.showToast({
                                                title: '服务器繁忙',
                                                icon:'error'
                                          })
                                    },
                                    complete:()=>{
                                          wx.hideLoading()
                                    }
                              })
                        } else if (res.cancel) {
                              
                        }
                  }
            })

      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            this.setData({
                  id:options.id
            },()=>{
                  this.init();
            })
      },
      submitComment(e){
            wx.showLoading({
              title: '评论中',
            })
            let comment={
                  userId:wx.getStorageSync('userId'),
                  content:e.detail.value,
                  mutualId:this.data.helpInfo.id
            }
            wx.request({
                  url: app.globalData.host+'client/user/putComment',
                  method:'PUT',
                  header:{
                        'content-type': 'application/x-www-form-urlencoded'
                  },
                  data:comment,
                  success:res=>{
                        this.setData({
                              commentList:res.data.data.commentList,
                              comment:''
                        })
                        
                  },
                  fail:()=>{
                        wx.showToast({
                          title: '服务器繁忙',
                        })
                  },
                  complete:()=>{
                        wx.hideLoading()
                  }
            })
            let val=e.detail.value;

      },

      init(){
            wx.showLoading({
              title: '加载数据',
            })
            wx.request({
                  url: getApp().globalData.host+'client/user/showHelp?id='+this.data.id,
                  success:res=>{
                        let info=res.data.data.helpInfo;
                        info.createTime=this.formatTime(info.createTime);
                        info.imgArr=JSON.parse(info.imgArr);
                        this.setData({
                              helpInfo:info,
                              commentList:res.data.data.commentList
                        })
                  },
                  fail:()=>{
                        wx.showToast({
                          title: '服务器繁忙'
                        })
                  },
                  complete:()=>{
                        wx.hideLoading();
                  }
            })
            this.setData({
                  userId:wx.getStorageSync('userId')
            })
      },
      
      formatTime(e){
            var time =  new Date(parseInt(e));
            var m = time.getMonth()+1;
            var d = time.getDate();
            let t= m+'月'+d+'日'+' '+time.getHours()+'时'+time.getMinutes()+'分';
            return t;
      },
      // 点击评论按钮
      onComment(){
            this.setData({
                  inputFocus:true
            },()=>{
                  // wx.onKeyboardHeightChange(res => {
                  //       console.log(res.height)
                  //       this.setData({
                  //             inputBottom:res.height
                  //       })
                  // })
            })
      },  
      // 失去焦点触发 
      handleBlur(){
            this.setData({
                  inputFocus:false
            })
      },
      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {

      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {

      }
})